<template>
    <div>
        <el-upload class="upload-demo" ref="uploadImport" :http-request="httpRequest" action=""
            :accept="acceptType === 'thesis' ? '.pdf' : '.jpg, .png'" :limit="1"
            :on-exceed="handleExceed" :file-list="fileList" :on-change="onChangeUpload">
            <el-button size="small" type="success" plain>点击上传</el-button>
            <div slot="tip" class="el-upload__tip">只能上传一个{{ acceptType === 'thesis' ? '.pdf' : '.jpg, .png' }}文件</div>
        </el-upload>
    </div>
</template>

<script>
// import { Message } from 'element-ui';

import apis from '@/utils/api';
import axios from 'axios';
import { Message } from 'element-ui';

export default {
    name: 'tapUpload',
    props: {
        acceptType: {
            type: String,
            default: ''
        }
    },
    data() {
        return {
            fileList: []
        }
    },
    methods: {
        onChangeUpload(file) {
            console.log(file);
        },
        handleExceed() {
            this.$message.warning('一次只能上传一个文件');
        },
        httpRequest(param) {
            const formData = {
                file: param.file
            }
            console.log('fd', formData, param);

            axios.post(`/api` + apis.uploadFile, formData, {
                headers: { 'Content-Type': 'multipart/form-data' },//定义内容格式,很重要
                timeout: 20000,
            }).then(res => {
                const {
                    code,
                    data,
                    message
                } = res.data.data;
                if (code === 200) {
                    this.$emit('change', data);
                    Message.success(message);
                } else {
                    Message.warning(message);
                }
            })
        }
    }
}    
</script>